﻿using System;
using System.Text;
using System.Web;
using LHB.Personal.Website.Label;
using VTemplate.Engine;
using LHB.Personal.Website.BLL;
using LHB.Personal.Website.Common;

namespace LHB.Personal.Website.WebUI
{
    public class TemplateEngine
    {
        private readonly HttpContext _context = HttpContext.Current;

        public string RenderHtml(string templateFile, object templateData)
        {
            string templateDir = "/Template/" + SettingBll.ReadSetting("SiteTemplate") + "/";

            var html = "";

            //try
            //{
                TemplateDocument document = new TemplateDocument(_context.Server.MapPath(templateDir + templateFile),
                    Encoding.UTF8, TemplateDocumentConfig.Full);
                PageLabel pageLabel = new PageLabel();
                document.SetValue("TD", pageLabel);
                document.SetValue("TDPage", templateData);
                html = document.GetRenderText();


            //}
            //catch (Exception e)
            //{
            //    TemplateDocument document = new TemplateDocument(_context.Server.MapPath(templateDir + "Error/500/index.html"),
            //        Encoding.UTF8, TemplateDocumentConfig.Full);
            //    PageLabel pageLabel = new PageLabel();
            //    document.SetValue("TD", pageLabel);
            //    document.SetValue("TDPage", new {ErrorInfo = e});
            //    html = document.GetRenderText();
            //}

            return html;
        }
    }
}